<%@ page import="main.java.com.entity.Category" %>
<%@ page import="java.util.List" %>
<%@ page import="main.java.com.entity.Dish" %><%--
  Created by IntelliJ IDEA.
  User: 20496
  Date: 2024/12/19
  Time: 14:13
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>套餐添加页面</title>
    <script src="js/jquery-3.7.1.js"></script>
    <style>
        body {
            margin: 0;
            padding: 0;
        }
        form{
            margin-left: 500px;
        }
        label{
            margin-top: 40px;
        }
        #dishesModal{
            //
        }
    </style>
    <script>
        $(document).ready(function () {
            $("form").submit(function (e) {
                e.preventDefault();
                // 可以在这里添加更多的前端验证逻辑，比如图片格式验证等（此处简单示例只检查必填项）
                var setmealId = $("#setmealId").val();
                var name = $("#name").val();
                var category_id = $("#category_id").val();
                var price = $("#price").val();
                var image = "backend/image/noImg.png";
                var description = $("#description").val();

                //获取添加套餐中的菜品ID并保存
                var addDishIds = [];
                $("input[name='dishId']:checked").each(function () {
                    addDishIds.push($(this).val());
                    addDishIds.push(",");
                });
                request.setAttribute("addDishIds", addDishIds);


                if (!setmealId ||!name ||!category_id ||!price ||!image ||!description) {
                    alert("所有项都为必填项，请检查输入！");
                    return false;
                }
                this.submit();
            });

            // 检查后端是否传递了添加成功的提示信息，如果有则展示在页面上
            var addSuccessMsg = "${addSuccess}";
            if (addSuccessMsg) {
                $(".success-message").html(addSuccessMsg);
            }
        });
    </script>
</head>
<body>
<form action="AddSetmealServlet" method="post">
    <label for="name">套餐ID：</label>
    <input type="text" id="setmealId" name="setmealId" required><br>
    <label for="name">套餐名称：</label>
    <input type="text" id="name" name="name" required><br>
    <label for="category_id">套餐分类：</label>
    <select id="category_id" name="category_id" required>
        <%
            List<Category> categories = (List<Category>) request.getAttribute("categories");
            if (categories!= null) {
                for (Category category : categories) {
                    out.println("<option value='" + category.getId() + "'>" + category.getName() + "</option>");
                }
            }
        %>
    </select><br>
    <label for="price">套餐价格：</label>
    <input type="text" id="price" name="price" required><br>
    <label>套餐菜品：</label>
    <div>
        <div id="dishesModal">
            <table>
                <tr>
                    <th><input type="checkbox" id="selectAllCheckbox"></th>
                    <th>名称</th>
                    <th>图片</th>
                    <th>售价</th>
                </tr>
                <%
                    List<Dish> dishes = (List<Dish>) request.getAttribute("dishes");
                    if (dishes!= null) {
                        for (Dish dish : dishes) {
                %>
                <tr>
                    <td><input type="checkbox" name="addDishIds" value="<%=dish.getId()%>"></td>
                    <td><%=dish.getName()%></td>
                    <td><img src="backend/image/<%=dish.getImage()%>" alt="菜品图片" width="50px" height="50px"></td>
                    <td><%=dish.getPrice()%></td>
                </tr>
                <%
                        }
                    }
                %>
            </table>
        </div>
    </div>
    <label for="image">套餐图片：</label>
    <input type="file" id="image" name="image" required><br>
    <label for="description">套餐描述：</label>
    <textarea id="description" name="description"></textarea><br>
    <input type="submit" value="保存">
</form>
</body>
</html>